/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package utils.tns;

import exceptions.NoTnsAdminFileException;
import processing.objects.Server;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

/**
 * Class for parsing TNS_NAMES.ora File
 *
 * @author Paweł Jasionowski, modified by rymek
 *         User: rymek
 *         Date: 30.06.11
 *         Time: 10:10
 */
public class GetTNS {

    private static String oraclePath = null;                                // oracle client installation path
    private static final Map<String, Server> mapDb = new TreeMap<>();  // map containing all available servers

    /**
     * Method for parsing TNS_NAMES.ora file containing database information
     *
     * @throws IOException             thrown when I/O error occurs
     * @throws NoTnsAdminFileException thrown when no ORACLE_HOME system variable is set
     */
    public static void getTns() throws NoTnsAdminFileException, IOException {

        if (oraclePath == null) {
            getOracleHomePath();
        }

        TnsParser tnsParser = new TnsParser(new File(oraclePath));

        for (String sid : tnsParser.getDatabaseNames()) {
            Server server = new Server();

            server.setTnsName(sid);
            server.setUrl(tnsParser.getHost(sid));
            server.setPort(tnsParser.getPort(sid));

            mapDb.put(sid, server);
        }
    }

    /**
     * Method for setting path to TNS_NAMES.ora file
     *
     * @throws NoTnsAdminFileException thrown when no ORACLE_HOME system variable is set
     */
    private static void getOracleHomePath() throws NoTnsAdminFileException {

        if (oraclePath == null) throw new NoTnsAdminFileException();
    }

    /**
     * Sets file path for TNS_NAMES.ora file
     *
     * @param oraclePath path to TNS_NAMES.ora file
     */
    public static void setOraclePath(String oraclePath) {
        GetTNS.oraclePath = oraclePath;
    }

    /**
     * Gets Oracle Client Install Path
     *
     * @return Oracle Client Install Path
     */
    public static String getOraclePath() {
        return oraclePath;
    }

    /**
     * Returns Map of available database servers
     *
     * @return map containing all available database environments
     */
    public static Map<String, Server> getMapDb() {
        return mapDb;
    }

    /**
     * Resets oracle client path
     */
    public static void resetPath() {
        oraclePath = null;
    }

}
